package com.jhws.util;
import com.jhws.sys.user.bo.UserCommsBo;

import java.util.List;

public class commUtil {
	public static String appendZero(String commCode){
		if (Integer.valueOf(commCode) < 10)
		{
			commCode = "000" + Integer.valueOf(commCode);
		}
		else if (Integer.valueOf(commCode) < 100)
		{
			commCode = "00" + Integer.valueOf(commCode);
		}
		else if (Integer.valueOf(commCode) < 1000)
		{
			commCode = "0" + Integer.valueOf(commCode);
		}
		return commCode;
	}
	
	public static String commCodesToSqlStr(String commCodes) {
		if(commCodes == null)
			return null;
		String [] commstr = commCodes.split(",");
		String comms = new String();
		for(String comm : commstr) {
			comms += "'"+comm+"'"+",";
		}
		String commWhere = comms.substring(0, comms.length() -1);
		return commWhere;
	}

	public static String userCommsToSqlStr(List<UserCommsBo> list){
		String comms = "";
		for(int i=0;i<list.size();i++){
			comms += "'"+list.get(i).getCommCode();
			if( i+1 < list.size())
				comms += "',";
			else
				comms += "'";
		}
		return comms;
	}

	/* 通过围墙机账号来定位到围墙机别名字符串中的位置
	*
	*  因为别名是逗号隔开的子串，如 devA,devB,devC
	*  假如传入的是00100000000b, 则自动提取第二个devB
	* */
	public static String getGateDevAliasName(String gateDevName, String gateDevAliasName) {
		String gateDevIndex = gateDevName.substring(gateDevName.length() - 1).toUpperCase();
		if(StringUtils.isNotEmpty(gateDevAliasName)){
			String [] aliasNames = gateDevAliasName.split(",");
			Integer index = (int)gateDevIndex.charAt(0) - (int)'A';
			if(aliasNames.length > index)
				return aliasNames[index];
		}

		return "围墙机-"+gateDevIndex;
	}

}
